package com.atguigu.guli.api.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.reactive.result.view.ViewResolver;
import org.springframework.web.server.ServerWebExchange;

import java.util.stream.Collectors;

@Configuration
public class GatewayConfig {

    //创建允许跨域访问的filter的对象 注入到容器中
    @Bean
    public CorsWebFilter corsWebFilter(){
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);//是否允许请求报文携带cookie
        config.addAllowedOrigin("*"); //允许异步访问的组织
        config.addAllowedMethod("*"); //允许异步访问的请求方式
        config.addAllowedHeader("*"); //允许所有的请求头访问
        //参数1：表示配置允许跨域访问的 路径 ， 参数2：具体的跨域配置
        source.registerCorsConfiguration("/**" , config );
        return new CorsWebFilter(source);
    }
}
